import { queryCurrent } from '@/services/user';

const GlobalModel = {
  namespace: 'global',
  state: {
    istop: false,
    savescroll: {},
    gotop: '',
    curtype: 'rfid',
    userinfo: {},
    token: null,
  },
  effects: {
    *istop({ payload }, { call, put, select }) {
      const savescroll = yield select((state) => state.global.savescroll);
      const curtype = yield select((state) => state.global.curtype);
      let newscroll = {
        ...savescroll,
        [curtype]: payload,
      };
      yield put({
        type: 'save',
        payload: { savescroll: newscroll },
      });
      yield put({
        type: 'save',
        payload: { istop: newscroll[curtype] },
      });
    },
    *curtype({ payload }, { call, put, select }) {
      const savescroll = yield select((state) => state.global.savescroll);
      yield put({
        type: 'save',
        payload: { curtype: payload },
      });
      yield put({
        type: 'save',
        payload: { istop: !!savescroll[payload] },
      });
    },
    *gotop({ payload }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: { gotop: payload },
      });
    },
    *token({ payload }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: { token: payload },
      });
    },

    *userinfo({ payload }, { call, put, select }) {
      let response = yield call(queryCurrent, payload);
      yield put({
        type: 'save',
        payload: { userinfo: response?.data ?? {} },
      });
      return response;
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
export default GlobalModel;
